package com.jmlee.controller;

import com.jmlee.pojo.ACategory;
import com.jmlee.pojo.CommonPage;
import com.jmlee.pojo.CommonResult;
import com.jmlee.service.ACategoryService;
import com.jmlee.util.SystemDateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ACategoryController {
    @Autowired
    private ACategoryService aCategoryService;

    @GetMapping("categoryManager")
    public String categoryManager(){
        return "categoryManager";
    }


    /**
     * 获取渲染分类列表
     * @param curPage
     * @param pageSize
     * @param aCategory
     * @return
     */
    @PostMapping("categoryList")
    @ResponseBody
    public CommonResult getCategoryList(String curPage, String pageSize, ACategory aCategory){
        CommonPage commonPage = new CommonPage();
        if (aCategory == null){
            commonPage.setData(aCategoryService.getAllCategory(new RowBounds((Integer.parseInt(curPage)-1)*Integer.parseInt(pageSize),Integer.parseInt(pageSize)),null));
        }else {
            commonPage.setData(aCategoryService.getAllCategory(new RowBounds((Integer.parseInt(curPage)-1)*Integer.parseInt(pageSize),Integer.parseInt(pageSize)),aCategory));
        }
        commonPage.setCount(aCategoryService.getCountOfCategory(aCategory));

        return CommonResult.success(commonPage,"success");
    }

    /**
     * 获取启用的父分类列表
     * @return
     */
    @PostMapping("getParentCategory")
    @ResponseBody
    public CommonResult getParentCategory(){
        ACategory aCategory = new ACategory();
        aCategory.setParentId(0L);
        List<ACategory> parentCategory = aCategoryService.getAllCategory(null,aCategory);
        return CommonResult.success(parentCategory,"success");
    }
    /**
     * 获取启用的子分类列表
     * @return
     */
    @PostMapping("getChildCategory")
    @ResponseBody
    public CommonResult getChildCategory(String parentId){
        ACategory aCategory = new ACategory();
        if (!parentId.equals("")) {
            aCategory.setParentId(Long.valueOf(parentId));
        } else {

        }
        List<ACategory> childCategory = aCategoryService.getAllCategory(null, aCategory);
        return CommonResult.success(childCategory, "success");
    }

    /**
     * 创建分类
     * @param aCategory
     * @return
     */
    @PostMapping("createNewCategory")
    @ResponseBody
    public String createNewCategory(@RequestBody ACategory aCategory){
        aCategory.setCreateTime(SystemDateUtil.getDaDate());
        return aCategoryService.addNewCategory(aCategory).toString();
    }

    /**
     * 修改分类
     * @param aCategory
     * @return
     */
    @PostMapping("editCategory")
    @ResponseBody
    public String editCategory(@RequestBody ACategory aCategory){
        return aCategoryService.editCategory(aCategory).toString();
    }/**
     * 启用分类
     * @param aCategory
     * @return
     */
    @PostMapping("enableCategory")
    @ResponseBody
    public String enableCategory(@RequestBody ACategory aCategory){
        aCategory.setStatus(1);
        return aCategoryService.editCategory(aCategory).toString();
    }/**
     * 禁用分类
     * @param aCategory
     * @return
     */
    @PostMapping("disableCategory")
    @ResponseBody
    public String disableCategory(@RequestBody ACategory aCategory){
        aCategory.setStatus(0);
        return aCategoryService.editCategory(aCategory).toString();
    }/**
     * 删除分类（逻辑删除）
     * @param aCategory
     * @return
     */
    @PostMapping("delCategory")
    @ResponseBody
    public String delCategory(@RequestBody ACategory aCategory){
        aCategory.setStatus(2);
        return aCategoryService.editCategory(aCategory).toString();
    }

    /**
     * 获取全部商品分类
     * @return
     */
    @PostMapping("getItemCategory")
    @ResponseBody
    public Map<String, Object> getItemCategory() {
        Map<String, Object> categoryMap = new LinkedHashMap<>();
        ACategory aCategory = new ACategory();
        aCategory.setParentId(0L);
        categoryMap.put("parentCategory", aCategoryService.getAllCategory(null, aCategory));
        categoryMap.put("childCategory", aCategoryService.getAllChildCategory());
        return categoryMap;
    }
}
